package com.lely.springcloud.controller;

import com.common.result.R;
import com.lely.springcloud.entity.Payment;
import com.lely.springcloud.service.PaymentService;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zzyy
 * @date 2020/2/18 10:43
 **/
@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;
    @Resource
    private DiscoveryClient discoveryClient;                                            //服务发现 获取服务信息

    /**
     * 新增
     * postman http://localhost:8001/payment/create?serial=lely002
     *
     * @param payment
     * @return
     */
    @PostMapping(value = "payment/create")
    public R create(Payment payment) {
        int result = paymentService.create(payment);
        log.info("*****插入结果: " + result);
        if (result > 0) {
            return R.ok(result, "插入数据库成功,serverPort:" + serverPort);
        }
        return R.error("插入数据库失败");
    }

    /**   查询  http://localhost:8001/payment/get/31  */
    @GetMapping(value = "payment/get/{id}")
    public R getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("*****查询结果: " + payment);
        if (payment != null) {
            return R.ok(payment, "查询成功,serverPort:" + serverPort);
        }
        return R.error("没有对应记录,查询ID:" + id);
    }

    /**   服务发现  */
    @GetMapping(value = "payment/discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            log.info("*****element:" + element);
        }
        // 一个微服务下的全部实例
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.debug(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + instance.getUri());
        }
        return this.discoveryClient;
    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB() {
        return serverPort;
    }


    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout() {
        try {
            // 暂停3秒钟
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    /**
     * 链路跟踪
     *
     * @return
     */
    // @GetMapping(value = "/payment/zipkin")
    // public String paymentZipkin() {
    //     return "hi,i'am paymentZipkin server fall back,welcome to lely,O(∩_∩)O哈哈~";
    // }
}
